#!/bin/bash
####################################################################
# Prey System Module Linux Functions - by Carlos Yaconi H.
# URL: http://preyproject.com
# License: GPLv3
####################################################################

system_udi=$(hal-find-by-property --key info.product --string Computer)

get_from_hal(){
	/usr/bin/hal-get-property --udi $system_udi --key $1
}

get_os_info(){
	os_version=`lsb_release -d | sed 's/.*:\t//'`
	kernel_version=`uname -r`
}

get_system_ids(){
	uuid=`get_from_hal system.hardware.uuid`
	serial_number=`get_from_hal system.hardware.serial`
	# if [ "$serial_number" == 'System Serial Number' ]; then # damn, generic shit
		# lets grab the MB serial number, maybe that works better
		# serial_number=`get_from_hal system.board.serial`
	# fi
}

get_mb_info(){
	mb_vendor=`get_from_hal system.board.vendor`
	mb_serial=`get_from_hal system.board.serial`
	mb_model=`get_from_hal system.board.product`
	mb_version=`get_from_hal system.board.version`
}

get_bios_info(){
	bios_vendor=`get_from_hal system.firmware.vendor`
	bios_version=`get_from_hal system.firmware.version`
}

get_cpu_info(){
	local cpu_info=$(cat /proc/cpuinfo)

	cpu_model=`echo -e "$cpu_info" | grep "model name" | tail -1 | sed 's/.*: //'`
	cpu_speed=`echo -e "$cpu_info" | grep "cpu MHz" | tail -1 | sed 's/.*: //'`
	cpu_cores=`echo -e "$cpu_info" | grep "processor" | wc -l`
}


get_memory_info(){
	local mem_in_kb=`cat /proc/meminfo | grep "MemTotal" | sed 's/.*:[[:space:]]*\(.*\) .*/\1/'`

	ram_size=$(($mem_in_kb/1024))
	ram_modules=`dmidecode --type 17 | grep Size | grep -v "No Module" | wc -l`
}

get_hdd_info(){
	local hard_disks=`cat /proc/partitions | awk 'BEGIN {

	ORS = "#"}
	{if (length($4) ==3) print $4}'`

	arr=$(echo $hard_disks | tr "#" "\n")
	it=0
	hdd_info=""
	for x in $arr; do
		it=$(($it+1))
		hd_size=`fdisk -l | grep "Disk /dev/$x" | sed 's/.*:[[:space:]]*\(.*\),.*/\1/'`
		hd_model=`hdparm -I "/dev/$x" | grep "Model Number" | sed 's/.*:[[:space:]]*\(.*\)/\1/'`
		hd_serial=`hdparm -I "/dev/$x" | grep "Serial Number" | sed 's/.*:[[:space:]]*\(.*\)/\1/'`
		hd_firmware=`hdparm -I "/dev/$x" | grep "Firmware Revision" | sed 's/.*:[[:space:]]*\(.*\)/\1/'`
		hdd_info="$hdd_info${it}_size=${hd_size}#${it}_model=${hd_model}#${it}_serial=${hd_serial}#${it}_firmware=${hd_firmware}__"
	done
	# echo -e $hdd_info
}
